<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <span class="search-title">审核日期</span>
      <el-date-picker
        v-model="checkTime"
        type="datetimerange"
        format="yyyy-MM-dd HH:mm"
        placeholder="选择日期时间"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :editable="false"
        :change="changeAttr()"
        :picker-options="pickerOptions">
      </el-date-picker>
      <span class="search-title">审核结果</span>
      <el-select class="filter-item" style="width: 130px" v-model="listQuery.check_result" placeholder="审核结果">
        <el-option v-for="item in arrs" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <el-button class="filter-item" @click="getList" type="primary" icon="el-icon-search">筛选</el-button>
    </div>
    <div style="clear:both"></div>
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="订单初审" name="first" v-if="per === '1' || per === '3'">
        <div class="filter-container">
          <component :is="historyList" :list="list" :type="activeName" :listLoading="listLoading"></component>
        </div>
      </el-tab-pane>
      <el-tab-pane label="订单复审" name="second" v-if="per === '2' || per === '3'">
        <div class="filter-container">
          <component :is="historyList" :list="list" :type="activeName" :listLoading="listLoading"></component>
        </div>
      </el-tab-pane>
    </el-tabs>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[5,10,20,30, 50]" :page-size="listQuery['per-page']" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<style scoped>
  .el-time-spinner__wrapper {
    vertical-align: inherit;
  }
  .filter-container .filter-item {
    margin-bottom: 4px;
  }
</style>
<script>
import { firstHistoryList, secondHistoryList } from '@/api/order'
import waves from '@/directive/waves' // 水波纹指令
import historylist from './components/historylist' // 订单列表页
import { hasOrderPermission } from '@/utils'

export default {
  name: 'complexTable',
  directives: {
    waves
  },
  data() {
    return {
      historyList: historylist,
      activeName: 'first',
      tableKey: 0,
      list: null,
      total: null,
      per: '3',
      listLoading: true,
      listQuery: {
        page: 1,
        'per-page': 10,
        check_time_begin: '',
        check_time_end: '',
        check_result: 0
      },
      checkTime: '',
      status: '',
      arrs: [
        { id: 0, name: '全部' },
        { id: 2, name: '通过' },
        { id: 1, name: '驳回' },
        { id: 3, name: '建议退款' },
        { id: 4, name: '退款' }
      ],
      pickerOptions: {
        disabledDate(time) {
          var d = new Date('2016-12-31')
          return time.getTime() > Date.now() || time.getTime() < d.getTime()
        }
      }
    }
  },
  created() {
    // 获取初审复审权限
    this.per = hasOrderPermission()
    if (this.per === '1') {
      this.activeName = 'first'
    } else if (this.per === '2') {
      this.activeName = 'second'
    } else if (this.per === '3') {
      this.activeName = 'first'
    }
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 判断是待初审还是待复审
      if (this.activeName === 'first') {
        this.arrs = [
          { id: 0, name: '全部' },
          { id: 2, name: '通过' },
          { id: 1, name: '驳回' },
          { id: 3, name: '建议退款' }
        ]
        firstHistoryList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      } else {
        this.arrs = [
          { id: 0, name: '全部' },
          { id: 2, name: '通过' },
          { id: 1, name: '驳回' },
          { id: 4, name: '退款' }
        ]
        secondHistoryList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      }
    },
    handleClick() {
      this.resetTemp()
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery['per-page'] = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    // 修改日期选择器时间
    changeAttr() {
      if (this.checkTime !== null) {
        this.listQuery.check_time_begin = this.checkTime[0]
        this.listQuery.check_time_end = this.checkTime[1]
      }
    },
    handleCreate() {

    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
      this.listQuery = {
        page: 1,
        'per-page': 10,
        check_time_begin: '',
        check_time_end: '',
        check_result: 0
      }
      this.checkTime = ''
    }
  }
}
</script>
